// pages/product/productinfo/productinfo.js
let AJAX = require('../../../utils/util.js').AJAX
const App = getApp()
let productKind = require('../../../utils/util.js').productKind
let formatRichText = require('../../../utils/util.js').formatRichText
let timeTranfer = require('../../../utils/util.js').timeTranfer
Page({

  /**
   * 页面的初始数据  
   */ 
  data: {
    proid: '',
    navH: '',
    dataInfo: '',
    activeNum: 'point',
    tabPosition: false,
    scrolltop: 0,
    chooseSize: false,
    chooseSize1: false,
    animationData: {},
    selected: 0,
    list: [],
    timeList: [],
    selected_time: 0,
    priceType: 0,
    priceTime: 0,
    dateTitle: '请选择出行时间',
    click_time: 0,
    sale_price: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      navH: App.globalData.navHeight,
      proid: options.id
    })
    this.requestInfo()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 时间选择
  selected_time_click: function(e) {
   
    let that = this
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;

    that.setData({
      selected_time: e.currentTarget.dataset.index,
      priceTime: item.timetable_id
    })

  },
  //tab框
  selected: function(e) {

    let that = this
    let index = e.currentTarget.dataset.index;

  
    that.setData({
      selected: e.currentTarget.dataset.index,
      priceTime: that.__data__.dataInfo.productSchedule[index].timeTable[0].timetable_id,
      priceType: that.__data__.dataInfo.productSchedule[index].schedule_title,
      sale_price: that.__data__.dataInfo.productSchedule[index].sales_price
    })

  },

  // 弹窗
  chooseSezi1: function(e) {
    // 用that取代this，防止不必要的情况发生

    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 500,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(200).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      chooseSize1: true,
      click_time: 1,

    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function() {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export()
      })
    }, 200)
  },
  chooseSezi: function(e) {
    // 用that取代this，防止不必要的情况发生

    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 500,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(200).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      chooseSize: true
    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function() {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export()
      })
    }, 200)
  },
  // 关闭弹窗
  hideModal: function(e) {
    var that = this;
    let type = e.currentTarget.dataset.type;
   
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(200).step()
    that.setData({
      animationData: animation.export()

    })
    setTimeout(function() {
      animation.translateY(0).step();
      that.setData({
        animationData: animation.export(),
        chooseSize: false,
        chooseSize1: false,
      
      });
      if (type == 'coupon' && that.data.priceType == 0) {
         
        that.setData({
          dateTitle: that.data.dateTitle,
        })
      } else {
        that.setData({
       
          dateTitle: that.data.priceType,


        })
      }
    }, 200)
  },
  // 关闭弹窗获取数据
  returnData: function() {
    let that = this;
    that.setData({
        chooseSize1: false
      }),
      wx.navigateTo({
        url: '/pages/order/orderwrite/orderwrite?id=' + this.data.proid + "&timeable_id=" + this.data.priceTime,
      })
  },
  // 获取详情

  requestInfo() {
    let vm = this
    AJAX("product/info", "GET", {
      id: vm.data.proid
    }).then(res => {
      let item = res.data
      // 保存商品列表
      let _list = []
      item.productSchedule.forEach((val, index) => {

        _list.push(val.schedule_title)
      })
      // 更改选择时间的时间戳        
      item.productSchedule.forEach((val, index) => {
      
        val.timeTable.forEach((v, index) => {
          v.timetable_time = timeTranfer(v.timetable_time, 1)
        })

      })
      // 更改时间戳
      item.coupons.forEach((val, index) => {
        val.created_at = timeTranfer(val.created_at, 1)
        val.start_time = timeTranfer(val.start_time, 1)
        val.end_time = timeTranfer(val.end_time, 1)
        val.price = parseInt(val.price)
        val.floor_price = parseInt(val.floor_price)
      })
      item.product_kind = productKind[item.product_kind || 1]
      item.theme_name = item.theme_name
      // console.log(item.productSchedule[0].sales_price)
      if (item.theme_name) {
        item.theme_name = item.theme_name.split('|')
      }
      if (item.productTrip) {
        item.productTrip.forEach(function(it, index) {
          item.productTrip[index].trip_content = formatRichText(it.trip_content).replace(/<section/g, '<div')
            .replace(/\/section>/g, '\div>');
        })
      }
      // 费用包含 dataInfo.productDetails.cost_list
      item.productDetails.cost_list = formatRichText(item.productDetails.cost_list).replace(/<section/g, '<div').replace(/\/section>/g, '\div>')
      item.productDetails.about_us = formatRichText(item.productDetails.about_us).replace(/<section/g, '<div').replace(/\/section>/g, '\div>')
      item.productDetails.notice = formatRichText(item.productDetails.notice).replace(/<section/g, '<div').replace(/\/section>/g, '\div>')

      if (item.productPoint) {
        item.productPoint.forEach(function(it, index) {
          item.productPoint[index].point_content = formatRichText(it.point_content).replace(/<section/g, '<div')
            .replace(/\/section>/g, '\div>');
        })
      }
      if (item.begin_time) {
        item.begin_time = timeTranfer(item.begin_time, 1)
      }    
       if (item.productCategory.product_type==1){
          vm.setData({
             dateTitle:'请选择课时'
          })
      }
      vm.setData({
        dataInfo: item,
        sale_price: item.productSchedule[0].sales_price,
         priceType: item.productSchedule[0].schedule_title,
        priceTime: item.productSchedule[0].timeTable[0].timetable_id,
        list: _list
      })

    })
  },
  // 领取优惠券
  receive(e) {
    console.log(e.currentTarget.dataset.id)
    AJAX('coupon-member/receive-coupon', 'POST', {
      coupon_id: e.currentTarget.dataset.id
    }).then(res => {
      if (res.status == 'succeed') {
        console.log("领取成功")
        wx.showToast({
          title: '领取成功',
          icon: "none"
        })

        //刷新当前页面的数据
        wx: wx.navigateTo({
          url: '/pages/product/productinfo/productinfo?id=' + this.data.proid,
          // success: function(res) {},
          // fail: function(res) {},
          // complete: function(res) {},
        })
      }


    })
  },
  tabActive(e) {
    let _type = e.currentTarget.dataset.type
    //consoe.log(_type)
    this.setData({
      activeNum: _type
    })
  },
  makePhone(e) {
    let _phone = e.currentTarget.dataset.phone
    if (!_phone) {
      return
    }
    wx.makePhoneCall({
      phoneNumber: _phone,
    })
  },
  writeOrder(e) {
    if (this.data.click_time == 0) {

      var that = this;
      // 创建一个动画实例
      var animation = wx.createAnimation({
        // 动画持续时间
        duration: 500,
        // 定义动画效果，当前是匀速
        timingFunction: 'linear'
      })
      // 将该变量赋值给当前动画
      that.animation = animation
      // 先在y轴偏移，然后用step()完成一个动画
      animation.translateY(200).step()
      // 用setData改变当前动画
      that.setData({
        // 通过export()方法导出数据
        animationData: animation.export(),
        // 改变view里面的Wx：if
        chooseSize1: true,
        click_time: 1,

      })
      // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
      setTimeout(function() {
        animation.translateY(0).step()
        that.setData({
          animationData: animation.export()
        })
      }, 200)



    } else {
      if (this.data.dataInfo.schedule_type == 1) {
        // 成品
        wx.navigateTo({
          url: '/pages/order/orderwrite/orderwrite?id=' + this.data.proid + "&timeable_id=" + this.data.priceTime,
        })
      } else {
        // 预售
        wx.navigateTo({
          url: '/pages/order/orderwrite/orderwrite?id=' + this.data.proid + "&timeable_id=" + this.data.priceTime,
        })
      }
    }



  },
  navBack() {
    wx.navigateBack()
  },
  srcollTap(e) {
    if (e.detail.scrollTop > 240) {
      this.setData({
        tabPosition: true
      })
    } else {
      this.setData({
        tabPosition: false
      })
    }
  },
  colTap(e) {
    let vm = this
    let _val = e.currentTarget.dataset.col == 1 ? 0 : 1
    AJAX("member-collection/execute", "POST", {
      product_id: vm.data.proid,
      execute_type: _val
    }).then(res => {
      wx.showToast({
        title: _val ? "收藏成功" : "取消收藏成功",
        icon: "none"
      })
      vm.requestInfo()
    })
  },
  codeRead(e) {
    let _url = e.currentTarget.dataset.url
    if (!_url) {
      return
    }
    wx.previewImage({
      urls: [_url],
    })
    // wx.getImageInfo({// 获取图片信息（此处可不要）
    //   src: _url,
    //   success: function (res) {
    //     debugger
    //   }
    // })
  },


})